class Admins::ReportsController < ApplicationController
  before_action :set_report, only: [:show, :edit, :update, :destroy,:submit,:approve,:reject]

  # GET /reports
  # GET /reports.json
  def index
    
    session[:department] = params[:department] if params[:department]
    session[:news_begin] = params[:news_begin] if params[:news_begin]
    session[:news_end] = params[:news_end] if params[:news_end]
    session[:report_id] = params[:report_id] if params[:report_id]
    session[:report_type] = params[:report_type] if params[:report_type]
    
    @departments = Department.all
    
    report_type = params[:report_type] || '1'
    @current_user = current_user
    @reports = Report.report_type(report_type,current_user).check(params[:check])
      .pubtime(params[:news_begin] || session[:news_begin],params[:news_end] || session[:news_end])
       .department(params[:department])
       .report_id(params[:report_id] || session[:report_id])
       .order("created_at desc").page(params[:page])
  end

  # GET /reports/1
  # GET /reports/1.json
  def show
  end

  # GET /reports/new
  def new
    if params[:report].blank?
      @report = Report.new
    else
      @report = Report.new(report_params)
    end
  end

  # GET /reports/1/edit
  def edit
  end

  # POST /reports
  # POST /reports.json
  def create
    @report = Report.new(report_params.merge({postby: current_user.name}))
    if @report.save
      redirect_to admins_reports_url, notice: '讲座报告创建成功'
    else
      flash[:error] = @report.errors.full_messages.join(",")
      redirect_to :action => :new,:report => @report.attributes
    end
  end

  def submit
    respond_to do |format|
      if @report.update_attributes(check: 1)
        format.html { redirect_to admins_reports_path @report, notice: '通告送审成功.' }
        format.json { render :show, status: :ok, location: @report }
      else
        format.html { render :edit }
        format.json { render json: @report.errors, status: :unprocessable_entity }
      end
    end
  end

  #采用
  def approve
    @report.update_attributes(check: 2)
    redirect_to admins_reports_path @report, notice: '讲座报告采用成功.'
  end

  #不采用
  def reject
    @report.update_attributes(check: 3,reason: params[:reason])
    redirect_to admins_reports_path @report, notice: '讲座报告退回成功.'
  end

  # PATCH/PUT /reports/1
  # PATCH/PUT /reports/1.json
  def update
    respond_to do |format|
      if @report.update(report_params)
        format.html { redirect_to admins_reports_url, notice: '讲座报告修改成功.' }
        format.json { render :show, status: :ok, location: @report }
      else
        flash[:error] = @report.errors.full_messages.join(",")
        format.html { render :edit }
        format.json { render json: @report.errors, status: :unprocessable_entity }
      end
    end
  end


  def change_time
    id = params[:id]
    time =params[:new_time]
    new =Report.find_by_id id
    if new
      if new.pubtime
        new.update_attributes(pubtime:Time.parse(time))
      else
        new.update_attributes(created_at:Time.parse(time))
      end
    end
    redirect_to '/admins/reports'
  end
  
  
  # DELETE /reports/1
  # DELETE /reports/1.json
  def destroy
    @report.destroy
    respond_to do |format|
      format.html { redirect_to admins_reports_url, notice: '讲座报告删除成功.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_report
      @report = Report.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def report_params
      params.require(:report).permit(:title, :place, :reporter, :time, :screen, :check, :content, :note, :writer, :postby, :fromwhere, :department, :orders, :pubtime)
    end
end
